import requests


def get_weather():
    url = 'https://xdl.zj121.com:8087/twsptfs-monitor/v1/monitor/realtime/'

    # 正确的请求体（JSON 格式）
    payload = {
        "locations": [
            {
                "longitude": 120.15,
                "latitude": 30.28
            }
        ]
    }

    # 添加必要的请求头
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    try:
        response = requests.post(
            url,
            json=payload,  # 使用 json 参数自动序列化为 JSON 并设置 Content-Type
            headers=headers,
            timeout=100
        )

        if response.status_code == 200:
            response_data = response.json()
            print("接口返回:", response_data)
            return response_data
        else:
            print(f"请求失败，状态码: {response.status_code}, 返回内容: {response.text}")
            return None  # 失败时返回 None 或空字典

    except requests.RequestException as e:
        print(f"请求错误: {e}")
        return None  # 异常时返回 None


if __name__ == "__main__":
    result = get_weather()
    print(result)